<?php

namespace App\Admin\Controllers\Dynamic;

use App\Admin\Repositories\Dynamic\Lists;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ListsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Lists(['tag','user']), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('user.name','用户');
            $grid->column('title');
            $grid->column('tag.name','标签');
            $grid->column('media_files')->if(function ($column){
                return $this->media_type == 'image' || empty($this->media_type);
            })->display(function ($p){
                return explode(',',$p);
            })->image('',60,60)->else()->video();
//                ->display(function ($p) {
//                    $html='';
//                    $arr=explode(',',$p);
//                    foreach ($arr as $item){
//                        $html.="<video width='60' height='60' controls='controls' src='{$item}'></video>";
//                    }
//                    return $html;
//            }) ;
//                ->modal(function ($modal) {
//                    // 设置弹窗标题
//                    $modal->title('预览');
//                    // 自定义图标
//                    $modal->icon('feather icon-x');
//                    $html='';
//                    $arr=explode(',',$this->media_files);
//                    foreach ($arr as $item){
//                        $html.="<video width='100' height='400' controls='controls' src='{$item}'></video>";
//                    }
//                    return "<div style='padding:10px 10px 0'>$html</div>";
//                });

            $grid->column('visible_range')->using([1=>'所有人可见',2=>'互关可见','自己可见']);
            $grid->column('status')->switch();
            $grid->column('praise_num');
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Lists(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('title');
            $show->field('tag_ids');
            $show->field('media_files');
            $show->field('visible_range');
            $show->field('status');
            $show->field('praise_num');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Lists(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('title');
            $form->text('tag_ids');
            $form->text('media_files');
            $form->text('visible_range');
            $form->switch('status');
            $form->text('praise_num');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
